
" 使能需要的程序包
:packadd! nerdtree " 加载NERDTree树插件
:packadd! nvim-lspconfig " 加载NVim 的 LSP 插件
" :log@2022/11/6
" 由于和v0.8.0版本冲突，所以屏蔽它
" :packadd! vim-airline " 加载vim-airline插件
:packadd! cmp-nvim-lsp 
:packadd! cmp-buffer
:packadd! nvim-cmp
:packadd! nvim-dap
" 启动XMAKE支持
" :packadd! x.vim
" :packadd! qrun.vim
" :packadd! xmake 

" For vsnip user.
:packadd! cmp-vsnip
:packadd! vim-vsnip

" 加载workstation
:packadd! workstation

" 使能Vim-Plug插件管理器
call plug#begin("v-plugin")
call plug#end()

" 显示行号
set nu

" 设置颜色
color default
" color darkblue

" 设置缩进为4空格
set autoindent
set cindent

set tabstop=4
set softtabstop=4

set shiftwidth=4

set cinoptions={0,1s,t0,n-2,p2s,(03s,=.5s,>1s,=1s,:1s

" 空格代替TAB
set expandtab

" 去掉VI兼容模式
set nocompatible

" 括号匹配
set showmatch

" 鼠标
set mouse=a
set selection=exclusive
set selectmode=mouse,key

" 高亮当前行
set cursorline

" 语法高亮
set syntax=on

" 设置默认的文件编码
set bomb
set fileencodings=utf-8,cp936

" 键位的映射
" 命令模式下 T 键出现NERDTree
noremap <C-S-a> :NERDTree<CR>
noremap <C-S-r> :NERDTreeRefreshRoot<CR>

" vim-airline 设置
" 全能缓冲区列表
let g:airline#extensions#tabline#enable = 1
" 只显示文件名
let g:airline#extensions#tabline#fnamemod = ':t'

" 使能rust的RLS语言服务
lua << EOF
require('init_nvim_cmp')

local nvim_lsp = require("lspconfig")

-- use an on_attach function to only map the following keys
-- after the languare server attaches to the current buffer

local on_attach = function(client, bufnr)
    local function buf_set_keymap(...)
        vim.api.nvim_buf_set_keymap(bufnr, ...)
	end

	local function buf_set_option(...)
	    vim.api.nvim_buf_set_option(bufnr, ...)
	end

	-- Enable complection triggered by <C-x><C-o>
	buf_set_option("omnifunc", "v:lua.lsp.omnifunc")

	-- Mappings.
	local opts = { noremap = true, silent = true}

	-- Set ':help vim.lsp.*' for documention on any of the below functions
	buf_set_keymap('n', 'gD', '<cmd>lua vim.lsp.buf.declaration()<CR>', opts)
	buf_set_keymap('n', 'gd', '<cmd>lua vim.lsp.buf.definition()<CR>', opts)
	buf_set_keymap('n', 'K', '<cmd>lua vim.lsp.buf.hover()<CR>', opts)
	buf_set_keymap('n', 'gi', '<cmd>lua vim.lsp.buf.implementation()<CR>', opts)
	buf_set_keymap('n', '<C-k>', '<cmd>lua vim.lsp.buf.signature_help()<CR>', opts)
	buf_set_keymap('n', '<space>wa', '<cmd>lua vim.lsp.buf.add_workspace_folder()<CR>', opts)
	buf_set_keymap('n', '<space>wr', '<cmd>lua vim.lsp.buf.remove_workspace_folder()<CR>', opts)
	buf_set_keymap('n', '<space>wl', '<cmd>lua print(vim.inspect(vim.lsp.buf.list_workspace_folders()))<CR>', opts)
	buf_set_keymap('n', '<space>D', '<cmd>lua vim.lsp.buf.type_definition()<CR>', opts)
	buf_set_keymap('n', '<space>rn', '<cmd>lua vim.lsp.buf.rename()<CR>', opts)
	buf_set_keymap('n', '<space>ca', '<cmd>lua vim.lsp.buf.code_action()<CR>', opts)
	buf_set_keymap('n', 'gr', '<cmd>lua vim.lsp.buf.references()<CR>', opts)
	buf_set_keymap('n', '<space>e', '<cmd>lua vim.lsp.diagnostic.show_line_diagnostics()<CR>', opts)
	buf_set_keymap('n', '[d', '<cmd>lua vim.lsp.diagnostic.goto_prev()<CR>', opts)
	buf_set_keymap('n', ']d', '<cmd>lua vim.lsp.diagnostic.goto_next()<CR>', opts)
	buf_set_keymap('n', '<space>q', '<cmd>lua vim.lsp.diagnostic.set_loclist()<CR>', opts)
	buf_set_keymap('n', '<space>f', '<cmd>lua vim.lsp.buf.formatting()<CR>', opts)
end

-- Use a loop to conveniently call 'setup' on multiple servers an 
-- map buffer local keybindings when the language server attaches
-- 
-- Install pylsp, use command : "pip install python-lsp-server -i https://pypi.tuna.tsinghua.edu.cn/simple"
-- Install rust_analyzer ：download from URL: "https://mirrors.tuna.tsinghua.edu.cn/github-release/rust-analyzer/rust-analyzer/LatestRelease/"
-- Install cmake lsp ："pip install cmake-language-server -i https://pypi.tuna.tsinghua.edu.cn/simple" 
-- 
local servers = { "rust_analyzer", "clangd", "pylsp", "cmake", "gopls" } --, "sumneko_lua" } 
for _, lsp in ipairs(servers) do
    local setup_opt = {
        on_attach = on_attach,
		flags = {
		    debounce_text_changes = 150,
		},
		capabilities = require('cmp_nvim_lsp').update_capabilities(vim.lsp.protocol.make_client_capabilities()),
		-- settings = {
		--    rust = {
		--		unstable_features = true,
		--		build_on_save = true,
		--		all_features = true
		--	}
		-- }	
    }
    if lsp == 'sumneko_lua' then
        setup_opt.cmd = { 'sumneko_lua' }
    end
	nvim_lsp[lsp].setup(setup_opt)
	 
end
EOF
